package com.zhongwang.mms.module.half.controller;

import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.module.half.model.HAdjustmentApplyModel;
import com.zhongwang.mms.module.half.model.HCheckDetailModel;
import com.zhongwang.mms.module.half.model.HCheckPlanModel;
import com.zhongwang.mms.module.half.model.HInventoryQueryModel;
import com.zhongwang.mms.module.half.service.HBasicService;
import com.zhongwang.mms.module.half.service.InventoryCheckService;
import com.zhongwang.mms.module.purchase.model.check.PCheckDetailQueryModel;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 盘点
 */
@Controller
@RequestMapping("/half/halInvManage/inventoryCheck")
public class InventoryCheckController extends BaseController {

    //页面文件路径
    private String prefix = "/half/halInvManage/inventoryCheck";

    @Autowired
    private InventoryCheckService inventoryCheckService;
    @Autowired
    private HBasicService hBasicService;


    /**
     * 加载页面
     * @return
     */
    @GetMapping("/page")
    @RequiresPermissions({"halinv:halcheckplan:view"})
    public String index() {
        return prefix + "/plan/page";
    }

    /**
     *@FunctionName: detail
     *@Description: 跳转到添加明细页
     *@Return: java.lang.String
     *@Version: 1.0.0
     */
    @GetMapping("/detail/{cpnId}")
    @RequiresPermissions({"halinv:halcheckadd:view"})
    public String detail(Model model, @PathVariable("cpnId") Integer cpnId) {
        model.addAttribute("cpnId", cpnId);
        model.addAttribute("mode", "add");
        model.addAttribute("warInfo", hBasicService.getWarInfo());
        model.addAttribute("cpnCode", inventoryCheckService.getCheckPlanByPrimary(cpnId).getCpnCode());
        return prefix + "/detail/page";
    }

    /**
     *@FunctionName: detail
     *@Description: 跳转到添加明细页
     *@CreateDate: 2020/4/25 13:55
     *@Return: java.lang.String
     *@Version: 1.0.0
     */
    @GetMapping("/detail/view/{cpnId}")
    @RequiresPermissions({"halinv:halcheckadd:view"})
    public String detailView(Model model, @PathVariable("cpnId") Integer cpnId) {
        model.addAttribute("cpnId", cpnId);
        model.addAttribute("mode", "view");
        model.addAttribute("cpnCode", inventoryCheckService.getCheckPlanByPrimary(cpnId).getCpnCode());
        return prefix + "/detail/page";
    }


    /**
     * 删除盘点明细
     * @param param
     * @return
     */
    @PostMapping("/detail/remove")
    @ResponseBody
    @RequiresPermissions({"halinv:halcheckadd:edit"})
    public JsonResult detailRemove(HCheckDetailModel param) {
        boolean result = inventoryCheckService.detailRemove(param);
        if (result) {
            return JsonResult.success();
        }
        return JsonResult.fail();
    }

    /**
     * 批量删除盘点明细
     * @param param
     * @return
     */
    @PostMapping("/detail/removes")
    @ResponseBody
    @RequiresPermissions({"halinv:halcheckadd:edit"})
    public JsonResult detailRemoves(HCheckDetailModel param) {
        return inventoryCheckService.detailRemoves(param);
    }

    /**
     *@FunctionName: detail
     *@Description: 页面调度中心
     *@Return: java.lang.String
     *@Version: 1.0.0
     */
    @GetMapping("/plan/dispatch/{mode}/{cpnId}")
    public String dispatch(Model model, @PathVariable("mode") String mode, @PathVariable("cpnId") Integer cpnId) {
        HCheckPlanModel checkPlanModel = inventoryCheckService.getCheckPlanByPrimary(cpnId);
        model.addAttribute("checkPlanModel", checkPlanModel);
        model.addAttribute("warInfo", hBasicService.getWarInfo());
        if (mode.equals("result")) {
            return prefix + "/result/page";
        }
        if (mode.equals("adjust")) {
            return prefix + "/adjust/page";
        }
        if (mode.equals("pal")) {
            return prefix + "/pal/page";
        }
        return prefix + "/detail/page";
    }

    /**
     * 加载盘点计划页面数据
     * @param hCheckPlanModel
     * @return
     */
    @ResponseBody
    @PostMapping("/plan/list")
    @RequiresPermissions({"halinv:halcheckplan:view"})
    public JsonResult list(HCheckPlanModel hCheckPlanModel) {
        startPage();
        List<HCheckPlanModel> list = inventoryCheckService.findCheckPlan(hCheckPlanModel);
        return jsonResult(list);
    }

    /**
     * 提交盘点计划
     * @param hCheckPlanModel
     * @return
     */
    @PostMapping("/plan/submit")
    @ResponseBody
    @RequiresPermissions({"halinv:halcheckplan:edit"})
    public JsonResult planSubmit(HCheckPlanModel hCheckPlanModel) {
        boolean result = inventoryCheckService.planSubmit(hCheckPlanModel);
        if (result) {
            return JsonResult.success();
        }
        return JsonResult.fail();
    }
    /**
     * 保存修改计划
     * @param hCheckPlanModel
     * @return
     */
    @PostMapping("/plan/save")
    @ResponseBody
    @RequiresPermissions({"halinv:halcheckplan:edit"})
    public JsonResult planSave(HCheckPlanModel hCheckPlanModel) {
        boolean result = inventoryCheckService.planSave(hCheckPlanModel);
        if (result) {
            return JsonResult.success();
        }
        return JsonResult.fail();
    }

    /**
     * 删除盘点计划
     *
     * @param param
     * @return json
     */
    @PostMapping("/plan/remove")
    @ResponseBody
    @RequiresPermissions({"halinv:halcheckplan:edit"})
    public JsonResult planRemove(HCheckPlanModel param) {
        boolean result = inventoryCheckService.planRemove(param);
        if (result) {
            return JsonResult.success();
        }
        return JsonResult.fail();
    }

    /**
     * 撤销盘点计划
     * @param param
     * @return
     */
    @PostMapping("/plan/revoke")
    @ResponseBody
    @RequiresPermissions({"halinv:halcheckplan:edit"})
    public JsonResult planRevoke(HCheckPlanModel param) {
        return inventoryCheckService.planRevoke(param);
    }

    /**
     * 根据盘点计划编码获取盘点明细列表
     * @param hCheckDetailModel
     * @return
     */
    @PostMapping("/detail/list")
    @ResponseBody
    @RequiresPermissions({"halinv:halcheckadd:view"})
    public JsonResult detailList(HCheckDetailModel hCheckDetailModel) {
        startPage();
        List<HCheckDetailModel> list = inventoryCheckService.findDetailList(hCheckDetailModel);
        return jsonResult(list);
    }

    /**
     * 弹出框数据列表
     * @param query
     * @return
     */
    @ResponseBody
    @PostMapping("/inventoryList")
    @RequiresPermissions({"halinv:halcheckadd:view"})
    public JsonResult inventoryList(HInventoryQueryModel query) {

        startPage();
        return jsonResult(inventoryCheckService.getInventoryList(query));
    }

    /**
     * 根据传过来的库存主键创建盘点明细
     * @param hCheckDetailModel
     * @return
     */
    @PostMapping("detail/create")
    @ResponseBody
    @RequiresPermissions({"halinv:halcheckadd:edit"})
    public JsonResult detailCreate(HCheckDetailModel hCheckDetailModel) {
        return inventoryCheckService.detailCreate(hCheckDetailModel);
    }

    /**
     * 盘点结果列表
     * @param hCheckDetailModel
     * @return
     */
    @PostMapping("/result/list")
    @ResponseBody
    @RequiresPermissions({"halinv:halcheckresult:view"})
    public JsonResult resultList(HCheckDetailModel hCheckDetailModel) {
        startPage();
        List<HCheckDetailModel> list = inventoryCheckService.findResultList(hCheckDetailModel);
        return jsonResult(list);
    }

    /**
     * 撤销盘点结果
     * @param param
     * @return
     */
    @PostMapping("/result/revoke")
    @ResponseBody
    @RequiresPermissions({"halinv:halcheckresult:edit"})
    public JsonResult resultRevoke(HCheckDetailModel param) {
        boolean result = inventoryCheckService.resultRevoke(param.getCdlCpnId());
        if (result) {
            return JsonResult.success("撤销盘点结果成功！");
        }
        return JsonResult.fail("撤销盘点结果失败");
    }

    /**
     * 单条盘点保存确定
     * @param hCheckDetailModel
     * @return
     */
    @PostMapping("/result/check")
    @ResponseBody
    @RequiresPermissions({"halinv:halcheckresult:edit"})
    public JsonResult resultCheck(HCheckDetailModel hCheckDetailModel) {
        if (inventoryCheckService.resultCheck(hCheckDetailModel)) {
            return JsonResult.success();
        }
        return JsonResult.fail();
    }
    /**
     * 提交盘点结果
     * @param hCheckDetailModel
     * @return
     */
    @PostMapping("/result/submit")
    @ResponseBody
    @RequiresPermissions({"halinv:halcheckresult:edit"})
    public JsonResult resultSubmit(HCheckDetailModel hCheckDetailModel) {
        return inventoryCheckService.resultSubmit(hCheckDetailModel.getCdlCpnId());
    }

    /**
     * 调整损益差和备注
     * @param hCheckDetailModel
     * @return
     */
    @ResponseBody
    @PostMapping("adjust/edit")
    @RequiresPermissions({"halinv:halcheckadj:edit"})
    public JsonResult adjustEdit(HCheckDetailModel hCheckDetailModel) {
        return inventoryCheckService.adjustEdit(hCheckDetailModel);
    }

    /**
     * 提交盘点损益调整
     * @param hCheckDetailModel
     * @return
     */
    @PostMapping("/adjust/submit")
    @ResponseBody
    @RequiresPermissions({"halinv:halchecksub:edit"})
    public JsonResult adjustSubmit(HCheckDetailModel hCheckDetailModel) {
        return inventoryCheckService.adjustSubmit(hCheckDetailModel.getCdlCpnId());
    }

    /**
     * 撤销盘点损益调整
     * @param hCheckDetailModel
     * @return
     */
    @PostMapping("/adjust/revoke")
    @ResponseBody
    @RequiresPermissions({"halinv:halcheckadj:edit"})
    public JsonResult adjustRevoke(HCheckDetailModel hCheckDetailModel) {
        return inventoryCheckService.adjustRevoke(hCheckDetailModel.getCdlCpnId());
    }

    /**
     * 盘点损益表列表
     * @param param
     * @return
     */
    @PostMapping("/pal/list")
    @ResponseBody
    @RequiresPermissions({"halinv:alchecksub:view"})
    public JsonResult palList(HAdjustmentApplyModel param) {
        return jsonResult(inventoryCheckService.palList(param));
    }

    /**
     * 获取汇总表信息
     * @param param
     * @return
     */
    @PostMapping("/pal/sumList")
    @ResponseBody
    @RequiresPermissions({"halinv:halcheckplan:view"})
    public JsonResult palSumList(HAdjustmentApplyModel param) {
        return jsonResult(inventoryCheckService.palSumList(param));
    }

    /**
     * 提交损益表
     * @param param
     * @return
     */
    @PostMapping("/pal/submit")
    @ResponseBody
    @RequiresPermissions({"halinv:halcheckadj:edit"})
    public JsonResult palSubmit(HAdjustmentApplyModel param) {
        return inventoryCheckService.palSubmit(param);
    }

    /**
     * 新增盘点
     * @param param
     * @return
     */
    @PostMapping("/result/checkAdd")
    @ResponseBody
    @RequiresPermissions({"halinv:halcheckplan:edit"})
    public JsonResult checkAdd(HCheckDetailModel param) {
        if (inventoryCheckService.checkAdd(param)) {
            return JsonResult.success();
        }
        return JsonResult.fail();
    }

    /**
     * 导出盘点计划
     * @param response
     * @param cpnId
     */
    @ResponseBody
    @GetMapping("/result/export/{cpnId}")
    @RequiresPermissions({"halinv:halcheckresult:edit"})
    public void resultExport(HttpServletResponse response, @PathVariable("cpnId") Integer cpnId) {
        inventoryCheckService.planExport(response, cpnId);
    }

    /**
     * 清空盘点结果
     * @param param
     * @return
     */
    @PostMapping("/result/clean")
    @ResponseBody
    @RequiresPermissions({"halinv:halcheckresult:edit"})
    public JsonResult resultClean(PCheckDetailQueryModel param) {
        if (inventoryCheckService.resultClean(param)) {
            return JsonResult.success();
        }
        return JsonResult.fail();
    }

    /**
     * 盘点结果导入
     * @param file
     * @return
     */
    @ResponseBody
    @PostMapping("result/import")
    @RequiresPermissions({"halinv:halcheckresult:edit"})
    public JsonResult resultImport(@RequestParam("file") MultipartFile file) {
        return success(inventoryCheckService.resultImport(file));
    }
}
